<%
   prop_out_name = property.out_name
   new_prefix = sprintf("%s%s%s", prefix, prefix.empty? ? "" : "_", prop_out_name)
   func_def = sprintf("adjust_%s(parent_input, parent_cur):", new_prefix)
-%>
<% if property.is_a?(Api::Type::Array) -%>


def <%= func_def %>
    iv = parent_input.get("<%= prop_out_name %>")
    if not (iv and isinstance(iv, list)):
        return

    cv = parent_cur.get("<%= prop_out_name %>")
    if not (cv and isinstance(cv, list)):
        return

<%   unless property.item_type.is_a?(Api::Type::NestedObject) -%>
    result = []
    for iiv in iv:
        if iiv not in cv:
            break

        result.append(iiv)

        j = cv.index(iiv)
        cv[j] = cv[-1]
        cv.pop()

    if cv:
        result.extend(cv)
    parent_cur["<%= prop_out_name %>"] = result
<%   else -%>
<%     identities = property.identities -%>
    lcv = len(cv)
<%     if identities.nil? -%>
    for i in range(len(iv)):
        if i >= lcv:
            break

        icv = cv[i]
        iiv = iv[i]
<%     else -%>
    result = []
    q = []
    for iiv in iv:
        if len(q) == lcv:
            break

        icv = None
        for j in range(lcv):
            if j in q:
                continue

            icv = cv[j]
<%       identities.each do |k| -%>

<%         p = find_property(property, [k]) -%>
<%         if p.required -%>
            if iiv["<%= k %>"] != icv["<%= k %>"]:
                continue
<%         else -%>
            if iiv["<%= k %>"] and (iiv["<%= k %>"] != icv["<%= k %>"]):
                continue
<%         end -%>
<%       end -%>

            result.append(icv)
            q.append(j)
            break
        else:
            break
<%     end -%>
<%     property.child_properties.each do |prop|
         c = adjust_option(prop, new_prefix, "iiv", "icv", 8, object, target)
-%>
<%       unless c.empty? -%>

<%= c %>
<%       end -%>
<%     end -%>

<%     unless identities.nil? -%>
    if len(q) != lcv:
        for i in range(lcv):
            if i not in q:
                result.append(cv[i])

    if len(result) != lcv:
        raise Exception("adjust property(<%= prop_out_name %>) failed, "
                        "the array number is not equal")

    parent_cur["<%= prop_out_name %>"] = result
<%     end -%>
<%     property.child_properties.each do |prop| -%>
<%       next unless need_adjust?(prop, object, target) -%>
<%= lines(build_adjust_option_method(new_prefix, prop, object, target)) -%>
<%     end -%>
<%   end -%>
<% elsif property.is_a?(Api::Type::NestedObject) -%>


def <%= func_def %>
    iv = parent_input.get("<%= prop_out_name %>")
    if not (iv and isinstance(iv, dict)):
        return

    cv = parent_cur.get("<%= prop_out_name %>")
    if not (cv and isinstance(cv, dict)):
        return
<%   property.child_properties.each do |prop|
       c = adjust_option(prop, new_prefix, "iv", "cv", 4, object, target)
-%>
<%     unless c.empty? -%>

<%= c %>
<%     end -%>
<%   end -%>
<%   property.child_properties.each do |prop| -%>
<%     next unless need_adjust?(prop, object, target) -%>
<%= lines(build_adjust_option_method(new_prefix, prop, object, target)) -%>
<%   end -%>
<% end # if property.is_a?(Api::Type::Array) -%>
